﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Agile.Engine.View;

namespace Fis.Win
{
    public class UIManager
    {

        /// <summary>
        /// 弹出提示
        /// </summary>
        /// <param name="text"></param>
        public static void AlertShow(string text)
        {
            var ac = new DevExpress.XtraBars.Alerter.AlertControl();
            ac.Show(null, "提示", text);
        }

        /// <summary>
        /// 显示子窗口
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="provider"></param>
        public static void ShowList<T>(string provider, System.Collections.Hashtable model = null)
            where T : Fis.Win.Forms.BaseForm, Agile.Engine.View.IView, new()
        {
            //var t = typeof(T);
            //Agile.Engine.View.IView f;
            ////存在激活当前窗口
            //if ((f = GetView(t.Name)) != null)
            //{
            //    (f as Form).Activate();
            //    return;
            //}

            //显示窗口
            var vp = Fis.Win.Utils.AppSession.GetViewProvider(provider);
            var v = vp.CreateView<T>(ViewState.View, model);
            v.ShowDialog();
        }

        public static IView ShowEdit<T>(string provider, ViewState state = ViewState.AddNew, System.Collections.Hashtable model = null)
            where T : Fis.Win.Forms.BaseForm, Agile.Engine.View.IView, new()
        {
            //显示窗口
            var vp = Fis.Win.Utils.AppSession.GetViewProvider(provider);
            var v = vp.CreateView<T>(state, model);
            v.ShowDialog();
            return v;
        }

        /// <summary>
        /// 获取已存在的窗口
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static Agile.Engine.View.IView GetView(string key)
        {
            foreach (var f in Application.OpenForms)
            {
                Agile.Engine.View.IView v = f as Agile.Engine.View.IView;
                if (v == null)
                    continue;
                if (v.KeyWords == key)
                {
                    return v;
                }
            }
            return null;
        }
    }
}
